home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------
- * $Header: /d1/dist/tools/RCS/chktime.c,v 1.2 1991/07/19 15:29:02 carlson Exp $
- *
- * chktime.c Compares two files modification times and returns -1, 0
- * or +1 depending on whether the first file was modified
- * before, at the same time or after the second file.
- *
- * Synopsis:
- * chktime [-d] [-r] <source-file> <object-file | executable-file>
- *
- * Description:
- * The purpose of this routine is to determine if a particular
- * source file needs to be recompiled. Since MCS does not use
- * the features of make to determine this need, this routine
- * had to be written.
- *
- * The functions performed by chktime are as follows:
- * 1. If -r option given, skip to step 3.
- * 2. If the source file exists in the current directory:
- * a. Set MODTIM to the last modified time of the file.
- * b. Skip to step 5.
- * 3. If the source file does not exist in ./RCS as an RCS
- * file, exit with an error message.
- * 4. Set MODTIM to the last modified time of the latest
- * revision in the RCS file.
- * 5. If MODTIM is less than or equal to the modification
- * time of the object file, return with a -1 or 0.
- * 6. Return with status of 1.
- *
- * The option -d will enable debug output.
- *
- * Revision history:
- * $Log: chktime.c,v $
- * Revision 1.2 1991/07/19 15:29:02 carlson
- * Added Header for RCS. Fixed up documentation.
- *
- * Revision 1.1 90/10/09 17:14:11 chris
- * Initial revision
- *
- * 15 Aug 1989 Christopher W. Carlson, Silicon Graphics Inc.
- * Initial draft
- *
- *--------------------------------------------------------------------*/
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <time.h>
- #include <ctype.h>
-
- static struct stat s_stat, o_stat;
- static char cbuff[256], dbuff[256];
- static enum { FALSE, TRUE } debug, rcs_only;
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
- register int i, j;
- register char *cpos;
- register struct tm *tmptr;
- FILE *fp;
-
- i = 3; /* Expected number of parameters */
- debug = FALSE; /* Assume no debug */
- rcs_only = FALSE; /* Assume check for local file first */
-
- if (argc > 1) {
- for (j = 1; j < 3; j++) {
- if (*argv[j] == '-') {
- if (argv[j][1] == 'd') {
- debug = TRUE;
- i++;
- } else if (argv[j][1] == 'r') {
- rcs_only = TRUE;
- i++;
- } else {
- fprintf (stderr,
- "Unrecognized option %s ignored\n", argv[j]);
- }
- }
- }
- }
-
- /*----
- * Make sure there are enough parameters.
- *----*/
-
- if (argc < i) {
- fprintf (stderr,
- "Usage: chktime [-d] [-r] <source-file> <object-file>\n");
- exit (-99);
- }
-
- /*----
- * Make sure we can get the information about the object
- * file. Here we also get the information.
- *----*/
-
- if (stat (argv[i-1], &o_stat) < 0) {
- fprintf (stderr, "Object file error %d\n", errno);
- exit (-99);
- }
-
- /*----
- * Check if we can get the information about the file in
- * the local directory. If not, start processing RCS file.
- *----*/
-
- if (rcs_only || stat (argv[i-2], &s_stat) < 0) {
-
- /*----
- * Create a file name in cbuff which consists of the
- * path RCS/ followed by the file name with ,v appended
- * to it.
- *----*/
-
- strcpy (cbuff, "RCS/");
- strcat (cbuff, argv[i-2]);
- strcat (cbuff, ",v");
-
- if (debug)
- printf ("chktime: Opening RCS file %s\n", cbuff);
-
- if ((fp = fopen (cbuff, "r")) == NULL) {
- fprintf (stderr, "Source file error %d on %s\n", errno, cbuff);
- exit (-99);
- }
-
- while (strncmp (dbuff, "date", 4) != 0) {
- if (fgets (dbuff, 256, fp) == NULL) {
- fprintf (stderr, "Error reading RCS file %s - 1\n", cbuff);
- exit (-99);
- }
- }
-
- fclose (fp);
- cpos = &dbuff[4];
- while (isspace (*cpos))
- cpos++;
-
- if (debug)
- printf (" Found date line: %.17s\n", cpos);
-
- tmptr = localtime (&o_stat.st_mtime);
- ascftime (cbuff, "%y.%m.%d.%H.%M.%S", tmptr);
-
- if (debug)
- printf (" Converted time for object: %s\n", cbuff);
-
- i = strncmp (cpos, cbuff, 17);
- i /= (i > 0) ? i : -i;
- } else {
-
- if (debug) {
- printf ("chktime: Found local file %s\n", argv[i-2]);
- printf (" Modify time of source = %ld\n", s_stat.st_mtime);
- printf (" Modify time of object = %ld\n", o_stat.st_mtime);
- }
-
- i = (s_stat.st_mtime < o_stat.st_mtime) ? -1 :
- (s_stat.st_mtime > o_stat.st_mtime) ? 1 : 0;
- }
-
- if (debug)
- printf (" Returning a value of %d\n", i);
-
- exit (i);
- }
-